package com.woniuxy.userapp.app.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.commons.util.Result;
import com.woniuxy.commons.util.TokenToUserDtoUtil;
import com.woniuxy.commons.util.UserDto;
import com.woniuxy.commons.util.WebUtil;
import com.woniuxy.userapp.client.StudentClient;
import com.woniuxy.userapp.client.param.AddStudentInfo;
import com.woniuxy.userapp.client.param.StudentInfo;
import com.woniuxy.userapp.common.exception.Myexception.UserException;
import com.woniuxy.userapp.common.exception.code.UserExceptionCode;
import com.woniuxy.userapp.infra.model.mapper.model.FrontAccount;
import com.woniuxy.userapp.infra.model.mapper.model.Student;
import com.woniuxy.userapp.infra.model.mapper.model.Venue;
import com.woniuxy.userapp.service.FrontAccountService;
import com.woniuxy.userapp.service.StudentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  学院
 * </p>
 *
 * @author 
 * @since 2022-08-03
 */
@RestController
public class StudentController implements StudentClient {

    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentClient studentClient;
    @Autowired
    private FrontAccountService frontAccountService;

    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();


    public Result<Integer> findAccountIdByStudentId(Integer stuentId){
        Student student = studentService.getOne(new QueryWrapper<Student>().eq("id", stuentId));
        if(student == null) throw new UserException(UserExceptionCode.STUDENT_NOT_EXISTST);
        return Result.getInstance(Integer.class).setCode(200).setData(student.getFrontAccountId());
    }

    @Override
    public Result<StudentInfo> findStudentByAccountId(Integer accountId) {
        Student student = studentService.getOne(new QueryWrapper<Student>().eq("front_account_id", accountId));
        if(student == null) throw new UserException(UserExceptionCode.STUDENT_NOT_EXISTST);
        StudentInfo studentInfo = BeanUtil.copyProperties(student, StudentInfo.class);
        return Result.getInstance(StudentInfo.class).setCode(200).setMsg("查询成功").setData(studentInfo);
    }

    /**
     * 完善学生信息
     * @param addStudentInfo
     * @return
     */
    @Override
    public Result addStudent(AddStudentInfo addStudentInfo) {
        String header = WebUtil.getRequest().getHeader("user");
        JSONObject entries = new JSONObject(header);
        Integer account_id = Integer.valueOf(entries.getStr("LoginId"));
        Result<StudentInfo> studentResult = studentClient.findStudentByAccountId(account_id);
        //判断该学员是否存在
        if(studentResult.getData() == null) throw new UserException(UserExceptionCode.STUDENT_NOT_EXISTST);

        //先在账户表中查询是否有此qq.email,phone,再更新账户表中的数据,最后跟心教练表
        //判断电话是否重复
        String phone = addStudentInfo.getPhone();
        FrontAccount frontAccount = frontAccountService.getOne(new QueryWrapper<FrontAccount>().eq("phone", phone));
        //如果输入电话所属用户不是自己,抛出有此用户
        if(frontAccount != null){
            if(frontAccount.getId() != account_id)throw new UserException(UserExceptionCode.PHONE_IS_EXISTS);
        }
//        if(frontAccount != null)throw new UserException(UserExceptionCode.PHONE_IS_EXISTS);
        //判断邮箱是否重复
        String email = addStudentInfo.getEmail();
        FrontAccount frontAccount1 = frontAccountService.getOne(new QueryWrapper<FrontAccount>().eq("email", email));
        if(frontAccount1 != null){
            if(frontAccount1.getId() != account_id)throw new UserException(UserExceptionCode.EMAIL_IS_EXISTS);
        }
//        if(frontAccount1 != null) throw new UserException(UserExceptionCode.EMAIL_IS_EXISTS);
        //判断qq号是否重复
        String qq = addStudentInfo.getQq();
        FrontAccount frontAccount2 = frontAccountService.getOne(new QueryWrapper<FrontAccount>().eq("qq", qq));
        if(frontAccount2 != null){
            if(frontAccount2.getId() != account_id) throw new UserException(UserExceptionCode.QQ_IS_EXISTS);
        }
//        if(frontAccount2 != null) throw new UserException(UserExceptionCode.QQ_IS_EXISTS);

        //账户表更新
        FrontAccount updateFrontAccount = new FrontAccount();
        updateFrontAccount.setId(account_id);
        updateFrontAccount.setQq(qq);
        updateFrontAccount.setEmail(email);
        updateFrontAccount.setPhone(phone);
        updateFrontAccount.setWalletPwd(encoder.encode(addStudentInfo.getWalletPwd()) );
        frontAccountService.saveOrUpdate(updateFrontAccount,new QueryWrapper<FrontAccount>().eq("id",account_id));

        //用户表更新
        addStudentInfo.setFrontAccountId(account_id);
        Student student = BeanUtil.copyProperties(addStudentInfo, Student.class);
        boolean save = studentService.saveOrUpdate(student, new UpdateWrapper<Student>().eq("front_account_id", account_id));
        if(!save) throw new UserException(UserExceptionCode.VENUE_ADD_FAILS);
        return Result.getInstance().setMsg("新增学生信息成功").setCode(200);
    }

}

